//
//  UIViewController+FCswizzling.m
//  辅城
//
//  Created by WJC on 2017/6/27.
//  Copyright © 2017年 吴 剑川. All rights reserved.
//

#import "UIViewController+FCswizzling.h"
//#import "FCHomeLoginViewController.h"
//#import "FCLoginViewController.h"
//#import "FCRegisterPhoneNumViewController.h"
//#import "FCRessetPasswordViewController.h"
//#import "FCGetVerifyCodeViewController.h"
//#import "FCInputVerifyCodeViewController.h"
//#import "FCSelectCountryViewController.h"
//#import "FCRegionsViewController.h"
#import "UIViewController+runtimeCheck.h"
#import <objc/runtime.h>
#import "WJCDefine.h"
//#import <UMCommon/MobClick.h>
#import "AFNetworkReachabilityManager.h"
#define locaChanged(key, annotation) localChanged(key, annotation, @"package")
@implementation UIViewController (FCswizzling)
#define IOS10 [[UIDevice currentDevice].systemVersion floatValue] >= 10
//+ (void)load{
//    //方法交换应该被保证，在程序中只会执行一次
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        
//        //获得viewController的生命周期方法的selector
//        SEL systemSel = @selector(viewDidAppear:);
//        //自己实现的将要被交换的方法的selector
//        SEL swizzSel = @selector(swiz_viewWillAppear:);
//        //两个方法的Method
//        Method systemMethod = class_getInstanceMethod([self class], systemSel);
//        Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
//        
//        //首先动态添加方法，实现是被交换的方法，返回值表示添加成功还是失败
//        BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
//        if (isAdd) {
//            //如果成功，说明类中不存在这个方法的实现
//            //将被交换方法的实现替换到这个并不存在的实现
//            class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
//        }else{
//            //否则，交换两个方法的实现
//            method_exchangeImplementations(systemMethod, swizzMethod);
//        }
//        
//        
//            //获得viewController的生命周期方法的selector
//        SEL systemDisappearSel = @selector(viewWillDisappear:);
//            //自己实现的将要被交换的方法的selector
//        SEL swizzDisappearSel = @selector(swiz_viewWillDisAppear:);
//            //两个方法的Method
//        Method systemDisappearMethod = class_getInstanceMethod([self class], systemDisappearSel);
//        Method swizzDisappearMethod = class_getInstanceMethod([self class], swizzDisappearSel);
//        
//            //首先动态添加方法，实现是被交换的方法，返回值表示添加成功还是失败
//        BOOL isDisappearAdd = class_addMethod(self, systemDisappearSel, method_getImplementation(swizzDisappearMethod), method_getTypeEncoding(swizzDisappearMethod));
//        if (isDisappearAdd) {
//                //如果成功，说明类中不存在这个方法的实现
//                //将被交换方法的实现替换到这个并不存在的实现
//            class_replaceMethod(self, swizzDisappearSel, method_getImplementation(systemDisappearMethod), method_getTypeEncoding(systemDisappearMethod));
//        }else{
//                //否则，交换两个方法的实现
//            method_exchangeImplementations(systemDisappearMethod, swizzDisappearMethod);
//        }
//        
//            //获得viewController的生命周期方法的selector
//        SEL systemDidLoadSel = @selector(viewDidLoad);
//            //自己实现的将要被交换的方法的selector
//        SEL swizzDidLoadSel = @selector(swiz_viewDidLoad);
//            //两个方法的Method
//        Method systemDidLoadMethod = class_getInstanceMethod([self class], systemDidLoadSel);
//        Method swizzDidLoadMethod = class_getInstanceMethod([self class], swizzDidLoadSel);
//        
//            //首先动态添加方法，实现是被交换的方法，返回值表示添加成功还是失败
//        BOOL isDidLoadAdd = class_addMethod(self, systemDidLoadSel, method_getImplementation(swizzDidLoadMethod), method_getTypeEncoding(swizzDidLoadMethod));
//        if (isDidLoadAdd) {
//                //如果成功，说明类中不存在这个方法的实现
//                //将被交换方法的实现替换到这个并不存在的实现
//            class_replaceMethod(self, swizzDidLoadSel, method_getImplementation(systemDidLoadMethod), method_getTypeEncoding(systemDidLoadMethod));
//        }else{
//                //否则，交换两个方法的实现
//            method_exchangeImplementations(systemDidLoadMethod, swizzDidLoadMethod);
//        }
//        Method oriMethod = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));
//        Method repMethod = class_getInstanceMethod([self class], @selector(swiz_delloc));
//        method_exchangeImplementations(oriMethod, repMethod);
//        
//        Method warningMethod = class_getInstanceMethod([self class], NSSelectorFromString(@"didReceiveMemoryWarning"));
//        Method NewWarningMethod = class_getInstanceMethod([self class], @selector(NewdidReceiveMemoryWarning));
//        method_exchangeImplementations(warningMethod, NewWarningMethod);
//        
//        
//        //ios13present不是全屏的问题
//
//         Method systemPresent =class_getInstanceMethod([self class],@selector(presentViewController:animated:completion:));
//
//       Method custom_Method =class_getInstanceMethod([self class],@selector(myPresentViewController:animated:completion:));
//
//      method_exchangeImplementations(systemPresent, custom_Method);
//    });
   
//}
-(void)myPresentViewController:(UIViewController*)viewControllerToPresent animated:(BOOL)flag completion:(void(^)(void))completion {


if(@available(iOS 13.0, *)) {
    if ([viewControllerToPresent isKindOfClass:NSClassFromString(@"WJCBaseViewController")]) {
         viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
    }
//    if ([viewControllerToPresent isKindOfClass:NSClassFromString(@"INFIBasePresentViewController")]) {
//        viewControllerToPresent.modalPresentationStyle = UIModalPresentationPopover;
//    }
    
}
    
    @try {
        if (self == viewControllerToPresent) {
            return;
        }
        [self myPresentViewController:viewControllerToPresent animated:flag completion:completion];
    } @catch (NSException *exception) {
        
        @throw exception;
    }
    

}
-(void)NewdidReceiveMemoryWarning
{
//此处做兼容处理需要加上ios6.0的宏开关，保证是在6.0下使用的,6.0以前屏蔽以下代码，否则会在下面使用self.view时自动加载viewDidUnLoad
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be needed later.
            // 加一些代码保存可能会需要用到的数据。
            // Add code to clean up other strong references to the view in the view hierarchy.
            //清理view里所有的强引用对象。
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
            
        }else{
//            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (void)swiz_viewDidLoad{
    if([self isKindOfClass:NSClassFromString(@"WJCBaseViewController")]){
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
     WJCLog(@"%@ didLoad加载",NSStringFromClass([self class]));
    }
    [self swiz_viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkWithSwit:) name:@"netWorkChanged" object:nil];
}
-(void)refreshData{
    [self refreshData];
    WJCLog(@"网络监听");
}
    //网络监听,如果需要刷新网络的话v控制器中添加refreshWorkData方法;
-(void)netWorkWithSwit:(NSNotification*)notification{
    if (notification.object[@"status"]) {
        if ([notification.object[@"status"] integerValue]==AFNetworkReachabilityStatusUnknown||
            [notification.object[@"status"] integerValue]==AFNetworkReachabilityStatusReachableViaWiFi||
            [notification.object[@"status"] integerValue]==AFNetworkReachabilityStatusReachableViaWWAN) {
//判断一个方法是否存在，然后执行
            SEL sel = NSSelectorFromString(@"refreshWorkData");
            if ([self respondsToSelector:sel]) {
                [self performSelector:sel];
            }
        }
    }
}
-(void)swiz_delloc{
    WJCLog(@"%@ delloc释放",NSStringFromClass([self class]));
     [self swiz_delloc];
}
- (void)swiz_viewWillAppear:(BOOL)animated{
    [self swiz_viewWillAppear:animated];
}
- (void)swiz_viewWillDisAppear:(BOOL)animated{
    [self swiz_viewWillDisAppear:animated];

    
}
-(void)showNaviWithLogntitude:(double)longtitude andLatitude:(double)Latitude{
    NSMutableArray *maps = [NSMutableArray array];
    // 创建UIActionSheet
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择导航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *baiduAction = [UIAlertAction actionWithTitle:@"使用百度地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",Latitude,longtitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

    }];
    UIAlertAction *gaodeAction = [UIAlertAction actionWithTitle:@"使用高德地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"辅城",@"FCApp",Latitude,longtitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
    }];
    UIAlertAction *googleAction = [UIAlertAction actionWithTitle:@"使用谷歌地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"辅城",@"FCApp",Latitude, longtitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        
    }];
    UIAlertAction *tencentAction = [UIAlertAction actionWithTitle:@"使用腾讯地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=终点&coord_type=1&policy=0",Latitude,longtitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];


        
    }];
    //百度地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        [alert addAction:baiduAction];
    }
    
    //高德地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        [alert addAction:gaodeAction];
    }
    
    //谷歌地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
        [alert addAction:googleAction];
    }
    
    //腾讯地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
        [alert addAction:tencentAction];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    [alert addAction:cancel];
    if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]&&![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]&&![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]&&![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
        [self pushAppControllerWithControllerName:@"XMGNavViewController" andObjectData:@{@"longitude":@(longtitude),@"latitude":@(Latitude)} andTitle:@"导航"];
    }
    else{
        [self presentViewController:alert animated:YES completion:nil];
    }
   
}
-(struct currentIdendity)getCurrentIdendityWithAlertNotice:(BOOL)notice{
//    FCEmployeeModel *emp=[FCEmployeeModel shareMineInformation];
//    UIAlertController *alert=nil;
    struct currentIdendity current;
//    if (!emp.CompanyId) {
//        alert=[UIAlertController alertControllerWithTitle:nil message:locaChanged(@"pay_homepage_personal_create_title", @"个人或未认证商户无法使用当前功能") preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *commitAction=[UIAlertAction actionWithTitle:locaChanged(@"company_card_business_create",@"创建商户") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//            [self performObjbect:NSClassFromString(@"FCCreatComapnyPushTool") andMethod:@"pushCreatCompanyControllerWithController:" withObjects:self,nil];
//
//
////[self pushAppControllerWithControllerName:@"FCGetCompanyInfoViewController" andObjectData:nil andTitle:@"认证企业"];
//        }];
//        UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:locaChanged(@"global_general_cancel",@"取消") style:UIAlertActionStyleCancel handler:nil];
//        [alert addAction:commitAction];
//        [alert addAction:cancelAction];
//        current.currentCompany=companyIdendityPersonal;
//        current.currentEmployee=empIdendityPersonal;
//    }
//   else if ([emp.Company.VerifyStatus integerValue]<1) {
//         alert=[UIAlertController alertControllerWithTitle:nil message:locaChanged(@"pay_homepage_business_create_title",@"未认证商户无法使用当前功能") preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *commitAction=[UIAlertAction actionWithTitle:locaChanged(@"global_ios_prompt4_twenty_one",@"认证商户") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//     [self pushAppControllerWithControllerName:@"FCAuthenticationViewController" andObjectData:nil andTitle:@"认证企业"];
//        }];
//        UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:locaChanged(@"global_general_cancel",@"取消") style:UIAlertActionStyleCancel handler:nil];
//        [alert addAction:commitAction];
//        [alert addAction:cancelAction];
//       if ([emp.Company.OwnerId isEqual:emp.UserId]) {
//           current.currentCompany=companyIdendityUncerty;
//           current.currentEmployee=empIdendityCompanyOwner;
//
//       }else{
//           current.currentCompany=companyIdendityUncerty;
//           current.currentEmployee=empIdendityCompanyEmployer;
//       }
//    }
//   else  if ([emp.Company.VerifyStatus integerValue] == 1) { //已认证
//        if (emp.Company.NotAllPass == 0) {//企业
//            if ([emp.Company.OwnerId isEqual:emp.UserId]) {
//                current.currentCompany=companyIdendityCompany;
//                current.currentEmployee=empIdendityCompanyOwner;
//            }else{
//                current.currentCompany=companyIdendityCompany;
//                current.currentEmployee=empIdendityCompanyEmployer;
//            }
//        }
//        else {//商户
//       if (emp.Company.IsIndividual) {
//           if ([emp.Company.OwnerId isEqual:emp.UserId]) {
//               current.currentCompany=companyIdendityIndividual;
//               current.currentEmployee=empIdendityCompanyOwner;
//           }else{
//               current.currentCompany=companyIdendityIndividual;
//               current.currentEmployee=empIdendityCompanyEmployer;
//           }
//
//       }
//       else{
//           if ([emp.Company.OwnerId isEqual:emp.UserId]) {
//               current.currentCompany=companyIdendityBusiness;
//               current.currentEmployee=empIdendityCompanyOwner;
//           }else{
//           current.currentCompany=companyIdendityBusiness;
//           current.currentEmployee=empIdendityCompanyEmployer;
//           }
//       }
//        }
//    }
//   else if ([emp.Company.VerifyStatus integerValue] == 2){
//       if ([emp.Company.OwnerId isEqual:emp.UserId]) {
//           current.currentCompany=companyIdenditycerting;
//           current.currentEmployee=empIdendityCompanyOwner;
//       }else{
//           current.currentCompany=companyIdenditycerting;
//           current.currentEmployee=empIdendityCompanyEmployer;
//       }
//   }
//    if(notice&&alert){
//        [self presentViewController:alert animated:YES completion:nil];
//    }
    return current;
}
@end
